/**
 * 
 */
package GUI;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.Font;
import java.awt.Color;
import javax.swing.JComboBox;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;

import Entity.Schedule;
import Entity.User;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

/**
 * @author aviat
 *
 */
public class EditShifts extends JPanel{

	
	private static final long serialVersionUID = 1L;
	private JButton btnBack;
	private JLabel iconlabel;
	private JLabel lblDate;
	private JLabel lblPlace;
	private JLabel lblContactName;
	private JLabel lblContactPhone;
	private JComboBox<String> PlacecomboBox;
	private JTextField ContactNametextField;
	private JTextField ContactPhonetextField;
	private JDatePickerImpl mdatePicker;
	private JComboBox<String> DatecomboBox;
	private JButton btnContinue;
	private JRadioButton rdbtnContactGroom;
	private JRadioButton rdbtnContactBride;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	

	private ArrayList<Schedule> m_allSchedule;
	private ArrayList<Schedule> m_filteredSchedule;
		private Object syncObject;
	
	private boolean m_isChangeAllowed;
	private JButton btnRefresh;
	/**
	 * Constructor
	 */
	public EditShifts() {
		setLayout(null);
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(EditShifts.class.getResource("/resources/back22.png")));
		ContactPhonetextField = new JTextField();
		ContactPhonetextField.setFont(new Font("Tahoma", Font.BOLD, 12));
		PlacecomboBox = new JComboBox<String>();
		PlacecomboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		PlacecomboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						filterList(PlacecomboBox.getSelectedItem().toString(),"EPLACE");
				}
			}
		});
		ContactNametextField = new JTextField();
		ContactNametextField.setFont(new Font("Tahoma", Font.BOLD, 12));
		DatecomboBox = new JComboBox<String>();
		DatecomboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		DatecomboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						filterList(DatecomboBox.getSelectedItem().toString(),"EDATE");
				}
			}
		});
		btnContinue = new JButton("Continue");
		btnContinue.setIcon(new ImageIcon(EditShifts.class.getResource("/resources/continueArrow.png")));
		rdbtnContactBride = new JRadioButton("Contact Bride");
		rdbtnContactBride.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		rdbtnContactBride.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					System.out.println(isChangeAllowed());
					if(isChangeAllowed())
						b_g_ToSet("BRIDE");
					
				}
			}
		});
		rdbtnContactGroom = new JRadioButton("Contact Groom");
		rdbtnContactGroom.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		rdbtnContactGroom.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						b_g_ToSet("GROOM");
					
				}
			}
		});
		
		initialize();
	}



	private void initialize() {
		JLabel lblEditShifts = new JLabel("Edit Shifts");
		lblEditShifts.setForeground(new Color(0, 204, 255));
		lblEditShifts.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblEditShifts.setBounds(10, 11, 143, 25);
		add(lblEditShifts);
		
		btnBack.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnBack.setBounds(10, 462, 101, 27);
		add(btnBack);
		
		ImageIcon LOGOImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resources/camera-background3.png"));
		ImageIcon LOGO = new ImageIcon (LOGOImageIcon.getImage().getScaledInstance(-1, -1, java.awt.Image.SCALE_SMOOTH));
		
		lblDate = new JLabel("Date");
		lblDate.setForeground(Color.WHITE);
		lblDate.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblDate.setBounds(208, 63, 126, 27);
		add(lblDate);
		
		lblPlace = new JLabel("Place");
		lblPlace.setForeground(Color.WHITE);
		lblPlace.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblPlace.setBounds(208, 122, 126, 27);
		add(lblPlace);
		
		lblContactName = new JLabel("Contact Name");
		lblContactName.setForeground(Color.WHITE);
		lblContactName.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblContactName.setBounds(208, 255, 140, 27);
		add(lblContactName);
		
		lblContactPhone = new JLabel("Contact Phone");
		lblContactPhone.setForeground(Color.WHITE);
		lblContactPhone.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblContactPhone.setBounds(208, 322, 140, 27);
		add(lblContactPhone);
		
		
		PlacecomboBox.setBounds(366, 123, 121, 27);
		add(PlacecomboBox);
		
		
		ContactNametextField.setBounds(366, 256, 121, 27);
		add(ContactNametextField);
		ContactNametextField.setColumns(10);
		
		
		ContactPhonetextField.setBounds(366, 323, 121, 27);
		add(ContactPhonetextField);
		ContactPhonetextField.setColumns(10);
		
		
		DatecomboBox.setBounds(366, 61, 121, 27);
		add(DatecomboBox);
		
		
		buttonGroup.add(rdbtnContactGroom);
		rdbtnContactGroom.setBounds(279, 188, 126, 27);
		add(rdbtnContactGroom);
		
		
		buttonGroup.add(rdbtnContactBride);
		rdbtnContactBride.setBounds(437, 188, 126, 27);
		add(rdbtnContactBride);
		
		btnContinue.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnContinue.setBounds(344, 394, 143, 26);
		add(btnContinue);
		
		btnRefresh = new JButton("Refresh");
		btnRefresh.setIcon(new ImageIcon(EditShifts.class.getResource("/resources/arrow-refresh-2-icon.png")));
		btnRefresh.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnRefresh.setBounds(529, 91, 121, 27);
		add(btnRefresh);
		
		iconlabel = new JLabel("");
		iconlabel.setIcon(LOGO);
		iconlabel.setBounds(0, 0, 786, 500);
		add(iconlabel);
	}

	/**
	 * @return the btnBack
	 */
	public JButton getBtnBack() {
		return btnBack;
	}

	/**
	 * @param btnBack the btnBack to set
	 */
	public void setBtnBack(JButton btnBack) {
		this.btnBack = btnBack;
	}




	/**
	 * @return the placecomboBox
	 */
	public JComboBox<String> getPlacecomboBox() {
		return PlacecomboBox;
	}

	/**
	 * @param placecomboBox the placecomboBox to set
	 */
	public void setPlacecomboBox(JComboBox<String> placecomboBox) {
		PlacecomboBox = placecomboBox;
	}

	/**
	 * @return the contactNametextField
	 */
	public JTextField getContactNametextField() {
		return ContactNametextField;
	}

	/**
	 * @param contactNametextField the contactNametextField to set
	 */
	public void setContactNametextField(JTextField contactNametextField) {
		ContactNametextField = contactNametextField;
	}

	/**
	 * @return the contactPhonetextField
	 */
	public JTextField getContactPhonetextField() {
		return ContactPhonetextField;
	}

	/**
	 * @param contactPhonetextField the contactPhonetextField to set
	 */
	public void setContactPhonetextField(JTextField contactPhonetextField) {
		ContactPhonetextField = contactPhonetextField;
	}

	/**
	 * @return the mdatePicker
	 */
	public JDatePickerImpl getMdatePicker() {
		return mdatePicker;
	}

	/**
	 * @param mdatePicker the mdatePicker to set
	 */
	public void setMdatePicker(JDatePickerImpl mdatePicker) {
		this.mdatePicker = mdatePicker;
	}


	/**
	 * @return the btnContinue
	 */
	public JButton getBtnContinue() {
		return btnContinue;
	}

	public void setSchedule(ArrayList<Schedule> allSched) {
		m_allSchedule = allSched;		
		m_filteredSchedule = new ArrayList<Schedule>();
		syncObject = new Object();
		filterList(null,"ALL");
	}
	
	private void filterList(String selectedItem, String scope)
	{
		synchronized (syncObject) {
			m_filteredSchedule.clear();
			switch(scope)
			{
			case "EDATE":
				for(Schedule s : m_allSchedule)
				{
					if(s.getDate().equals(selectedItem))
					{
						m_filteredSchedule.add(s);
					}
				}
				break;
			case "EPLACE":
				for(Schedule s : m_allSchedule)
				{
					if(s.getPlace().equals(selectedItem))
					{
						m_filteredSchedule.add(s);
					}
				}
				break;
			case "ALL":
				m_filteredSchedule.addAll(m_allSchedule);
				//wID_filteredList.addAll(wID_byRoleList);
				break;
			}
		}
		m_isChangeAllowed=false;
		filterComboBox(DatecomboBox,"EDATE");
		filterComboBox(PlacecomboBox,"EPLACE");
		m_isChangeAllowed=true;
	}
	private void filterComboBox(JComboBox<String> combo, String scope)
	{
		combo.removeAllItems();
		synchronized (syncObject) {
			
			for(Schedule s : m_filteredSchedule)
			{
				switch(scope)
				{
				case "EDATE":
					combo.addItem(s.getDate());
					break;
				case "EPLACE":
					combo.addItem(s.getPlace());
					break;
				}
			}
			
		}
		
	}
	
	private void b_g_ToSet(String scope){
		
		synchronized (syncObject) {
			for(Schedule s : m_allSchedule)
			{
				if(s.getDate().equals(String.valueOf(DatecomboBox.getSelectedItem()))){
					if(s.getPlace().equals(String.valueOf(PlacecomboBox.getSelectedItem())))
					{
					switch(scope)
						{
						case "BRIDE":
							ContactNametextField.setText(s.getbContactName());
							ContactPhonetextField.setText(s.getbContactPhone());
							break;
						case "GROOM":
							ContactNametextField.setText(s.getgContactName());
							ContactPhonetextField.setText(s.getgContactPhone());
							break;
						}
					}
				}
			}
				
		}
	}
	private boolean isChangeAllowed()
	{
		return m_isChangeAllowed;
	}

	/**
	 * @return the datecomboBox
	 */
	public JComboBox<String> getDatecomboBox() {
		return DatecomboBox;
	}


	/**
	 * @param datecomboBox the datecomboBox to set
	 */
	public void setDatecomboBox(JComboBox<String> datecomboBox) {
		DatecomboBox = datecomboBox;
	}


	/**
	 * @return the rdbtnContactGroom
	 */
	public JRadioButton getRdbtnContactGroom() {
		return rdbtnContactGroom;
	}


	/**
	 * @param rdbtnContactGroom the rdbtnContactGroom to set
	 */
	public void setRdbtnContactGroom(JRadioButton rdbtnContactGroom) {
		this.rdbtnContactGroom = rdbtnContactGroom;
	}


	/**
	 * @return the rdbtnContactBride
	 */
	public JRadioButton getRdbtnContactBride() {
		return rdbtnContactBride;
	}


	/**
	 * @param rdbtnContactBride the rdbtnContactBride to set
	 */
	public void setRdbtnContactBride(JRadioButton rdbtnContactBride) {
		this.rdbtnContactBride = rdbtnContactBride;
	}


	/**
	 * @return the buttonGroup
	 */
	public ButtonGroup getButtonGroup() {
		return buttonGroup;
	}

}
